<?php


namespace app\supplier\controller;


use app\erp\model\BrandApidb;
use app\erp\model\Brandcertdb;
use app\erp\model\Goodsdb;
use think\Db;

class Brandcert extends Base
{
    protected $cols = array(
        array(
            'field' => 'id',
            'width' => 40,
            'title' => 'ID',
            'type' => 'checkbox',
            'fixed' => 'left'
        ),
        array(
            'field' => 'card',
            'width' => 160,
            'title' => '授权编号'
        ),
        array(
            'field' => 'is_open',
            'width' => 100,
            'title' => '是否启用',
            'templet' => '#is_openTpl'
        ),
        array(
            'field' => 'state_tips',
            'width' => 120,
            'title' => '到期提醒',
            'templet' => '#state_tipsTpl'
        ),
        array(
            'field' => 'department',
            'width' => 160,
            'title' => '管理部门'
        ),
        array(
            'field' => 'create_time',
            'width' => 160,
            'title' => '申请时间'
        ),
        array(
            'field' => 'brand_name',
            'width' => 160,
            'title' => '品牌名称'
        ),
        array(
            'field' => 'files',
            'width' => 160,
            'title' => '授权文件',
            'templet'=>'#filesTpl'
        ),
        array(
            'field' => 'source',
            'width' => 160,
            'title' => '收集人'
        ),
        array(
            'field' => 'supplier_company',
            'width' => 160,
            'title' => '授权来源'
        ),
        array(
            'field' => 'levels',
            'width' => 160,
            'title' => '授权分级'
        ),
        array(
            'field' => 'levels_intro',
            'width' => 160,
            'title' => '分级补充',
        ),
        array(
            'field' => 'company',
            'width' => 160,
            'title' => '授权公司'
        ),
        array(
            'field' => 'companyed',
            'width' => 160,
            'title' => '被授权公司',
            'templet' => '#companyedTpl'
        ),
        array(
            'field' => 'tigongfang',
            'width' => 160,
            'title' => '授权提供方'
        ),
        array(
            'field' => 'start_date',
            'width' => 160,
            'title' => '授权开始时间'
        ),
        array(
            'field' => 'end_date',
            'width' => 160,
            'title' => '授权截止时间'
        ),
        array(
            'field' => 'channel',
            'width' => 160,
            'title' => '授权渠道'
        ),
        array(
            'field' => 'category_arr_view',
            'width' => 160,
            'title' => '授权品类范围'
        ),
        array(
            'field' => 'goods',
            'width' => 160,
            'title' => '授权商品'
        ),
        array(
            'field' => 'beizhu',
            'width' => 160,
            'title' => '运营备注'
        ),
        array(
            'field' => 'area',
            'width' => 160,
            'title' => '授权区域'
        ),
        array(
            'field' => 'audit_state',
            'width' => 80,
            'title' => '审核',
            'templet' => '#audit_stateTpl'
        ),
        array(
            'field' => 'audit_time',
            'width' => 160,
            'title' => '审核时间'
        ),
        array(
            'field' => 'reason',
            'width' => 160,
            'title' => '驳回原因'
        ),
        array(
            'fixed' => 'right',
            'width' => 110,
            'toolbar' => '#brandcert_tool'
        )
    );

    public $post = [];

    public function _initialize()
    {
        parent::_initialize();
        $request = request();
        $param = $request->param();
        $this->post = $param;
    }

    public function index()
    {
        $this->assign('cols', json_encode($this->cols));
        return $this->fetch();
    }

    public function ajaxList()
    {
        $brandcertdb = new Brandcertdb();
        $brandcertdb->param = $this->post;
        $brandcertdb->param['supplier_id'] = MEMBER_ID;
        $result = $brandcertdb->order('id desc')->paginate($this->post['limit']);
        $this->layuiReturn($result->items(), $result->total());
    }

    public function tplForm()
    {
        $id = 0;
        $data = array();
        if (isset($this->post['id'])) {
            $brandcertdb = new Brandcertdb();
            $data = $brandcertdb->find($this->post['id']);
            $id = $this->post['id'];
        }
        $chk_supplier = Db::name('member')->where('member_id', MEMBER_ID)->value('is_supplier');
        $this->assign('data', json_encode($data));
        $this->assign('id', $id);
        $this->assign('is_supplier', $chk_supplier);
        return $this->fetch();
    }

    /**
     * Title("获取品牌名称")
     */
    public function selectBrand(){
        $get_brand = Db::name('brand_api')
            ->whereNotNull('brand_name')
            ->whereLike('brand_name','%'.$this->post['brand_name'].'%')
            ->group('brand_name')
            ->limit('30')
            ->order('brand_name asc')
            ->column('brand_name');
        $this->layuiReturn($get_brand,count($get_brand));
    }


    /**
     * Title("获取一级城市分类")
     */
    public function selectArea()
    {
        $get_area = Db::name('city')->where('level', 1)->order('code asc')->column('name');
        $this->layuiReturn($get_area, count($get_area));
    }

    /**
     * Title("保存申请数据")
     */
    public function saveData()
    {
        $brandcertdb = new Brandcertdb();
        $this->post['supplier_id'] = MEMBER_ID;
        $get_member = Db::name('member')->where('member_id', MEMBER_ID)->field('supplier_name,is_supplier')->find();
        if ($get_member['is_supplier']) {
            $this->post['source'] = $get_member['supplier_name'];
        }
        if (isset($this->post['id']) && $this->post['id']) {
            $get_brandcert = $brandcertdb->find($this->post['id']);
            $get_brandcert->save($this->post);
            $this->upGoodsYybz($get_brandcert['id']);
        } else {
            unset($this->post['id']);
            // 生成证书编号
            $get_max_son = $brandcertdb->where('brand_name', $this->post['brand_name'])->field('son_sn,sn')->order('son_sn desc')->find();
            if ($get_max_son) {
                $this->post['sn'] = $get_max_son['sn'];
                $this->post['son_sn'] = $get_max_son['son_sn'] + 1;
            } else {
                $this->post['son_sn'] = 0;
                $get_max_sn = $brandcertdb->order('sn desc')->value('sn');
                if($get_max_sn) {
                    $this->post['sn'] = $get_max_sn + 1;
                } else {
                    $this->post['sn'] = 1;
                }
            }
            // 证书编号结束
            $brandcertdb->save($this->post);

        }
        $this->layuiReturn('success', 1);
    }


    /**
     * Title("同步运营备注")
     */
    public function upGoodsYybz($cert_id)
    {
        $brandcertdb = new Brandcertdb();
        $get_brandcert = $brandcertdb->find($cert_id);
        if ($get_brandcert) {
            if ($get_brandcert['audit_state'] == 1) {
                $goodsdb = new Goodsdb();
                $save_goods = [];
                $brandApidb = new BrandApidb();
                $get_brand = $brandApidb->where('brand_name', $get_brandcert['brand_name'])->field('brand_sign,option')->find();
                if ($get_brandcert['goods']) {
                    $jun_id_arr = explode(',', $get_brandcert['goods']);
                    $get_goods = $goodsdb
                        ->where('jun_brandid',$get_brand['option'])
                        ->whereIn('jun_id', $jun_id_arr)
                        ->select();
                } elseif ($get_brandcert['category_arr'] == 'all') {
                    $get_goods = $goodsdb
                        ->where('jun_brandid',$get_brand['option'])
                        ->select();
                } else {
                    $get_goods = $goodsdb
                        ->where('jun_brandid',$get_brand['option'])
                        ->whereIn('category_sign3', explode(',', $get_brandcert['category_arr']))
                        ->select();
                }
                //halt($goodsdb->getLastSql());
                foreach ($get_goods as $key => $val) {
                    $temp = [];
                    $temp['goods_id'] = $val['goods_id'];
                    $temp['yybz'] = $get_brandcert['beizhu'];
                    array_push($save_goods, $temp);
                }
                if (count($save_goods)) {
                    $goodsdb->saveAll($save_goods);
                }
            }
        }
    }

    /**
     * Title("设置启停开关")
     */
    public function setOpen(){
        $brandcertdb = new Brandcertdb();
        $get_brandcedrt = $brandcertdb->find($this->post['id']);
        $get_brandcedrt->save(['is_open'=>$get_brandcedrt['is_open']^1]);
        $this->success('success');
    }


}